<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\info;

use app\enum\CommonEnum;
use app\model\mongo\MongoInfoModel;
use app\model\mongo\MongoPicModel;
use app\model\mongo\MongoReportModel;
use app\service\analysis\info\kuaishou\PicInfoService as KS;
use app\service\analysis\info\ocean\PicInfoService as OE;
use app\service\analysis\info\qq\PicInfoService as QQ;
use app\service\analysis\material\PicMaterialService;
use app\service\BaseService;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

final class PicInfoService extends BaseService
{
    /**
     * @var null|InfoService 信息服务服务
     */
    private ?InfoService $service;

    public function __construct(int $mediaId = 0)
    {
        if ($mediaId == CommonEnum::OE_MEDIA) {
            $this->service = new OE();
        }
        if ($mediaId == CommonEnum::KS_MEDIA) {
            $this->service = new KS();
        }
        if ($mediaId == CommonEnum::QQ_MEDIA) {
            $this->service = new QQ();
        }
    }

    /**
     * 是否是信息服务
     */
    public function isService(): bool
    {
        return $this->service instanceof InfoService || self::setError('获取信息服务失败');
    }

    /**
     * 获取待执行报表.
     * @param int $page 当前页
     * @param int $limit 分页数
     * @return array|Collection|MongoReportModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getWaitSystemList(int $page = 1, int $limit = 200)
    {
        return $this->service::getList(['is_system' => MongoInfoModel::WAIT_FLUSH_MATERIAL], $page, $limit);
    }

    /**
     * 是否是系统图片.
     */
    public function isSystemMaterial(MongoInfoModel $model): bool
    {
        if (! $model->getAttr('signature')) {
            return false;
        }
        $obj = PicMaterialService::getPicBySignature($model->getAttr('signature'));
        return $obj instanceof MongoPicModel && $obj->getKey();
    }

    public function setSyncMaterial(MongoInfoModel $model){

    }
}
